//
//  VedioDownLoadViewController.m
//  UESTCPioneer
//
//  Created by Apple on 14-5-10.
//  Copyright (c) 2014年 Sway. All rights reserved.
//

#import "VedioDownLoadViewController.h"
#import "helper.h"
#import "LearnDetailViewController.h"
//#import "PartyDataLearnEntity.h"
#import "PartyVedioDownloadEntity.h"

@interface VedioDownLoadViewController (){

    //CoreData数据操作的上下文，负责所有的数据操作，类似于SQLite数据库的句柄。
    NSManagedObjectContext *_context;
}

@property (nonatomic,strong) NSArray * data;
/*最新添加*/
@property (nonatomic,strong) UILabel * fileLabel;
@property (nonatomic,strong) NSMutableArray * fileNameArray;
/**/
@property (nonatomic,strong) UILabel * downloadStatus;
@property (nonatomic,strong) VedioDownloadDetailViewController * detailController;
@end

@implementation VedioDownLoadViewController

static NSString * CellTableIdentifier = @"CellTableIdentifier";

- (id)initWithRequestData:(NSDictionary *)RequestData entityName:(NSString *)EntityName Mapping:(NSDictionary *)Mapping{
    if (self) {
        self.title = @"党课视频";
        requestData = RequestData;
        entityName = EntityName;
        entityMapping = Mapping;
//        [self downloadDataWithRequestData:RequestData  EntityName:EntityName Mapping:Mapping];
        
        //        NSLog(@"tableViewEntitiesArray = %@",tableViewEntitiesArray);
    }
    return self;
}



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
//    [self changeStatus];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorInset = UIEdgeInsetsZero;
}

-(void)viewWillAppear:(BOOL)animated{
    self.isVideo = YES;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [tableViewEntitiesArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier];
    }
    //NSDictionary * rowData = self.data[indexPath.row];
    PartyVedioDownloadEntity * entity = tableViewEntitiesArray[indexPath.row];
//    NSLog(@"视频下载 entity = %@",entity);
    UILabel * label;
    UILabel * statusLabel;
    if ([entity.isDownload  isEqual: @1]) {//该文件已下载过了
        
        label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 185, cell.frame.size.height)];
        
        statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(label.frame.origin.x + label.frame.size.width, 0, 50, cell.frame.size.height)];
        [statusLabel setText:@"[已下载]"];
        statusLabel.textColor = [UIColor redColor];
        [statusLabel setFont:[UIFont boldSystemFontOfSize:13.0f]];
//        [statusLabel setBackgroundColor:[UIColor yellowColor]];
        statusLabel.tag = 10;
        [cell.contentView addSubview:statusLabel];
    }else{//该文件没有下载过
        label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 235, cell.frame.size.height)];
        for (UIView * view in cell.contentView.subviews) {
            if (view.tag == 10) {
                [view removeFromSuperview];
            }
        }
    }
    
//    [label setBackgroundColor:[UIColor redColor]];
    label.text = entity.fileName;
    [cell.contentView addSubview:label];
    
    UIImage * leftImage = [helper getCustomImage:[UIImage imageNamed:@"dxfile.png"] insets:UIEdgeInsetsMake(45, 0, 90, 65)];
    UIImageView * imageView = [[UIImageView alloc] initWithImage:leftImage];
    imageView.frame = CGRectMake(10, 0, 30, cell.frame.size.height);
    [cell.contentView addSubview:imageView];
    cell.indentationLevel = 2;
    cell.indentationWidth = 20.0f;
    UIImage * rightImage = [helper getCustomImage:[UIImage imageNamed:@"learnDownload.png"] insets:UIEdgeInsetsMake(15, 0, 30, 15)];
    UIImageView * rightView = [[UIImageView alloc] initWithFrame:CGRectMake(280, 0, 30, cell.frame.size.height)];
    [rightView setImage:rightImage];
    [cell.contentView addSubview:rightView];
    
    // Configure the cell...
    
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PartyVedioDownloadEntity * entity = tableViewEntitiesArray[indexPath.row];
//    NSLog(@"**********************entity = %@",entity);
    NSString * urlStr;
    if ([self.title isEqualToString:@"党课视频下载"]) {
        urlStr = [baseUrl stringByAppendingString:[NSString stringWithFormat:@"/ScienceApp/FileDownServlet.do?filename=%@&majorType=video",entity.fileName]];
    }
    //    UIViewController * viewController = [[LearnDetailViewController alloc ] initWithFileName:entity.fileName URLString:urlStr];
    _detailController = [[VedioDownloadDetailViewController alloc ] initWithFileName:entity.fileName URLString:urlStr  filePath:entity.filePath  theId:entity.theId size:entity.size];
    _detailController.downLoadFinishDelegate = self;
    [self.navigationController pushViewController:_detailController animated:YES];
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
	return [tableViewEntitiesArray count];
}


-(void)downLoadFinishWithIndexPath:(NSNumber *)status  fileTheId:(NSNumber *)fileTheId{
//    NSLog(@"正在执行!");
    PartyVedioDownloadEntity * entity;
    for (int i = 0; i <tableViewEntitiesArray.count; i ++) {
        entity = tableViewEntitiesArray[i];
//        NSLog(@"entity = %@",entity);
//        NSLog(@"entity.theId = %@,fileTheId =%@",entity.theId,fileTheId);
        if([entity.theId isEqualToNumber:fileTheId]) {
//            NSLog(@"status = %@,entity.isDownload = %@",status,entity.isDownload);
            entity.isDownload = status;
            break;
        }   
    }
   [self.tableView reloadData];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//    NSLog(@"导航!");
    _detailController = segue.destinationViewController;
    _detailController.downLoadFinishDelegate = self;
}
/**/

@end
